<div class="memberships index">
    <p class='contentmenu'>
        <?php if(isset($permission['Membership']['Create']) && $permission['Membership']['Create']): ?>
            [ <?php echo $html->link(__("Add new member",true),array('committee'=>$dcommittee['Committee']['short_name'],'action'=>'add')); ?> ]
        <?php endif; ?>
        <?php if(isset($permission['Group']['Create']) && $permission['Group']['Create']): ?>
            [ <?php echo $html->link(__("Add new group",true),array('committee'=>$dcommittee['Committee']['short_name'],'controller'=>'groups','action'=>'add')); ?> ]
        <?php endif; ?>
    </p>
    
    <h2><?php __('Address Book');?></h2>
    <?php echo $this->element('searchbox',array('from'=>$this->params['controller'].'/'.$this->params['action']));?>

    <h4><?php __('Groups');?></h4>
    <table cellpadding="0" cellspacing="0">
        <tr>
            <th width="3%"><?php __('No')?></th>
            <th><?php echo __('Name',true);?></th>
            <?php if ($auth_user['User']['superuser'] || (isset($secretariat) && $secretariat)) { ?>
            <th class="actions" colspan=3><?php __('Actions');?></th>
            <?php } ?>
        </tr>
        <?php
            if(!count($groups)) echo "<tr><td colspan='5'>".__('No record found',true)."</td></tr>";
            $i=0;    
            foreach ($groups as $group):
                $class = null;
                if ($i++ % 2 == 0) {
                    $class = ' class="altrow"';
                }
        ?>
                <tr<?php echo $class;?>>
                    <td><?php echo $i.'. '; ?></td>
                    <td><?php echo $html->link($group['Group']['name'],array('committee'=>$dcommittee['Committee']['short_name'],'controller'=>'groups','action'=>'view','id'=>$group['Group']['id'])); ?></td>
                    <?php echo $this->element('crud',array('crudid'=>$group['Group']['id'],'permission'=>isset($permission['Group'])?$permission['Group']:null,'controller'=>'groups')) ?>
                </tr>
            <?php endforeach; ?>
    </table>
    
    <h4><?php __('Members');?></h4>
    <?php $total_record = $paginator->counter(array('format'=>'%count%'));?>
    <table cellpadding="0" cellspacing="0">
        <tr>
            <th><?php __('No')?></th>
            <th colspan='2'><?php echo $paginator->sort('name',null,array('url'=>array('committee'=>$dcommittee['Committee']['short_name'])));?></th>
            <th><?php echo $paginator->sort('email',null,array('url'=>array('committee'=>$dcommittee['Committee']['short_name'])));?></th>
            <th><?php echo $paginator->sort('job_title',null,array('url'=>array('committee'=>$dcommittee['Committee']['short_name'])));?></th>
            <th><?php __('Division')?></th>
            <th><?php __('Role')?></th>
            <?php if ($auth_user['User']['superuser'] || (isset($secretariat) && $secretariat)) { ?>
            <th class="actions" colspan='5'><?php __('Actions');?></th>
            <?php } ?>
        </tr>
        <?php
            if(!count($memberships)) 
                echo "<tr><td colspan='8'>".__('No record found',true)."</td></tr>";
            if ($this->params['paging']['Membership']['page'] == '1') $i = 0;
            else $i = $this->params['paging']['Membership']['options']['limit'] * $this->params['paging']['Membership']['page'] - $this->params['paging']['Membership']['options']['limit'];
            
            //check whether there are locked committee member
            $dlock=array_diff(Set::extract($memberships,'{n}.User.locked'),array('0000-00-00 00:00:00'));
            $lock=0;
            if(!empty($dlock)){
                foreach($dlock as $d){
                    if(date('Y-m-d H:i:s',strtotime($d))>date('Y-m-d H:i:s')){
                        $lock=1;
                    }
                }
            }
            
            foreach ($memberships as $membership):
                $class = null;
                if ($i++ % 2 == 0) {
                    $class = ' class="altrow"';
                }
        ?>
                <tr<?php echo $class;?>>
                    <td>
                        <?php echo $i.'. '; ?>
                    </td>
                    <td>
                        <?php 
                            if (isset($membership['User']['title_id']) && $membership['User']['title_id']!='0') {
                                $title = Set::extract("/Title[id=".$membership['User']['title_id']."]",$titles);
                                echo $title['0']['Title']['short_name'];
                            }
                        ?> 
                    </td>
                    <td>
                        <?php echo $html->link($membership['User']['name'],array('committee'=>$dcommittee['Committee']['short_name'],'controller'=>'memberships','action'=>'view','id'=>$membership['Membership']['id']));?>
                    </td>
                    <td>
                        <?php echo $membership['User']['email'] ? '<a href="mailto:'.$membership['User']['name'].'<'.$membership['User']['email'].'>">'.$membership['User']['email'].'</a>':'&nbsp;'; ?> 
                    </td>
                    <td>
                        <?php echo $membership['User']['job_title'] ? $membership['User']['job_title']:'&nbsp;'; ?> 
                    </td>
                    <td>
                        <?php echo $membership['User']['bahagian'] ? $membership['User']['bahagian']:'&nbsp;'; ?> 
                    </td>
                    <td>
                        <?php echo $membership['Role']['name'] ? $membership['Role']['name']:'&nbsp;'; ?> 
                    </td>
                    <?php if ($auth_user['User']['superuser'] || (isset($secretariat) && $secretariat)) { ?>
                    <td>
                        <?php if ( $i != 1 ) { echo $html->link($html->image('icons/go-up.gif',array('alt'=>__('Up',true),'title'=>__('Up',true))) ,array('committee'=>$dcommittee['Committee']['short_name'],'action'=>'moveup',$dcommittee['Committee']['id'],$membership['Membership']['id']),array('escape'=>false)); }else{ echo '&nbsp;';} ?>
                    </td>
                    <td>
                        <?php if ( $i != $total_record ) { echo $html->link($html->image('icons/go-down.gif',array('alt'=>__('Down',true),'title'=>__('Down',true))), array('committee'=>$dcommittee['Committee']['short_name'],'action'=>'movedown',$dcommittee['Committee']['id'],$membership['Membership']['id']),array('escape'=>false)); }else{ echo '&nbsp;';} ?>
                    </td>
                        <?php if($lock==1){ ?>
                        <td>
                            <?php if(date('Y-m-d H:i:s',strtotime($membership['User']['locked']))>date('Y-m-d H:i:s'))echo $html->link($html->image('icons/icon_reset.jpg',array('alt'=>__('Reset Password',true),'title'=>__('Reset Password',true))), array('controller'=>'users','action'=>'resetpass', 'id'=>$membership['User']['id'],'comm'=>$dcommittee['Committee']['short_name']),array('escape'=>false),sprintf(__('Are you sure you want to reset password for %s?', true).'\n'.__("Password will be reset to the username.",true),$membership['User']['username']));?>
                        </td>
                        <?php } ?>
                    <?php } ?>
                    <?php 
                        echo $this->element('crud',array('crudid'=>$membership['Membership']['id'],'permission'=>isset($permission['Membership'])?$permission['Membership']:null));
                    ?>
                </tr>
            <?php endforeach; ?>
    </table>
</div>
<div class="paging">
    <?php echo $paginator->prev('<< '.__('previous', true), array('url'=>array('committee'=>$dcommittee['Committee']['short_name'])), null, array('class'=>'disabled'));?>
 |     <?php echo $paginator->numbers(array('url'=>array('committee'=>$dcommittee['Committee']['short_name'])));?>
    <?php echo $paginator->next(__('next', true).' >>', array('url'=>array('committee'=>$dcommittee['Committee']['short_name'])), null, array('class'=>'disabled'));?>
</div>
